package com.intelligent.health.base.export.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.intelligent.health.base.excption.FileExportException;
import com.intelligent.health.base.export.ExportInterfaceService;
import com.intelligent.health.base.export.bean.ExportCheckGroupBean;
import com.intelligent.health.base.export.bean.ExportCheckItemBean;
import com.intelligent.health.base.export.bean.ExportSetMealBean;
import com.intelligent.health.base.utils.DateUtil;
import com.intelligent.health.base.utils.ExcelUtil;
import com.intelligent.health.setmeal.feign.SetMealFeignClient;
import com.intelligent.health.setmeal.pojo.SetMeal;
import entity.R;
import entity.constant.ExceptionConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/03/27/16:06
 * @Description: ExportSetMealImpl套餐导出接口实现
 */
@Slf4j
@Component(value = "exportSetMeal")
public class ExportSetMealServiceImpl implements ExportInterfaceService {

    @Autowired
    private SetMealFeignClient setMealFeignClient;

    @Override
    public void getExportBeanList(HashMap<String, Object> hashMap, HttpServletResponse response) throws FileExportException {
        //获取返回结果 result
        R result = setMealFeignClient.querySetMeals(hashMap);
        //获取ExportSetMealBean对象
        List<ExportSetMealBean> exportSetMealBeans = new ArrayList<>();
        ArrayList setMealList = JSONObject.parseObject(JSON.toJSONString(result.getData()), ArrayList.class);
        //setMeals集合遍历
        setMealList.forEach(setMeal -> {
            ArrayList<ExportCheckGroupBean> exportCheckGroupBeans = new ArrayList<>();
            //获取单个setMeal中的checkGroup集合
            ArrayList checkGroupList = JSONObject.parseObject(JSONObject.parseObject(JSON.toJSONString(setMeal)).getString("checkGroups"), ArrayList.class);
            ExportSetMealBean exportSetMealBean = JSONObject.parseObject(JSON.toJSONString(setMeal), ExportSetMealBean.class);

            //checkGroupList遍历
            checkGroupList.forEach(checkGroup -> {
                //类型转换 CheckGroup --> ExportCheckGroupBean
                ExportCheckGroupBean exportCheckGroupBean = JSONObject.parseObject(JSON.toJSONString(checkGroup), ExportCheckGroupBean.class);
                //获取单个checkGroup中的checkItem集合
                ArrayList checkItemList = JSONObject.parseObject(JSONObject.parseObject(JSON.toJSONString(checkGroup)).getString("checkItems"), ArrayList.class);
                List<ExportCheckItemBean> exportCheckItemBeans = new ArrayList<>();
                checkItemList.forEach(checkItem -> {
                    //类型转换 CheckItem --> ExportCheckItemBean
                    ExportCheckItemBean exportCheckItemBean = JSONObject.parseObject(JSON.toJSONString(checkItem), ExportCheckItemBean.class);
                    //将exportCheckItemBean添加到exportCheckItemBeans集合中
                    exportCheckItemBeans.add(exportCheckItemBean);
                });
                exportCheckGroupBean.setExportCheckItemBeans(exportCheckItemBeans);
                //将exportCheckGroupBean添加到exportCheckGroupBeans集合中
                exportCheckGroupBeans.add(exportCheckGroupBean);
            });
            exportSetMealBean.setExportCheckGroupBeans(exportCheckGroupBeans);
            exportSetMealBeans.add(exportSetMealBean);
        });
        System.out.println(exportSetMealBeans);
        //导出参数设置 设置样式
        ExportParams exportParams = new ExportParams();
        exportParams.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
        String dateString = DateUtil.getDateString();
        String fileName = dateString + ".xls";
        try {
            ExcelUtil.exportExcel(exportSetMealBeans,
                    "套餐记录表", "sheet1", ExportSetMealBean.class, fileName, true, response);
        } catch (Exception e) {
            log.error("套餐记录表导出异常: {}", hashMap);
            throw new FileExportException(ExceptionConst.FILE_EXPORT_EXCEPTION);
        }
    }
}
